package librus.control
{
	import flash.desktop.ClipboardFormats;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.NativeDragEvent;
	import flash.filesystem.File;
	import mx.managers.DragManager;
	import librus.model.Card;
	import librus.view.View;
	public class DragDropManager extends EventDispatcher
	{
		public static const FILES_ADDED:String = "filesAdded";
		
		public function DragDropManager(view:View)
		{
			__view = view;
			view.grid.addEventListener(NativeDragEvent.NATIVE_DRAG_ENTER, onDragIn);
			view.grid.addEventListener(NativeDragEvent.NATIVE_DRAG_DROP, onDragDrop);
		}
		
		private var __cards:Array;
		
		private var __view:View;
		
		public function get cards():Array
		{
			return __cards;
		}
		
		private function onDragDrop(e:NativeDragEvent):void
		{
			var arr:Array = e.clipboard.getData(ClipboardFormats.FILE_LIST_FORMAT) as Array;
			
			var finder:CardFinder = new CardFinder;
			__cards = finder.getCards(arr);
			
			dispatchEvent(new Event(FILES_ADDED));
		}

		private function onDragIn(e:NativeDragEvent):void
		{
			if(e.clipboard.hasFormat(ClipboardFormats.FILE_LIST_FORMAT))
			{
				var files:Array = e.clipboard.getData(ClipboardFormats.FILE_LIST_FORMAT) as Array;
				
				if(files.length > 0)
					DragManager.acceptDragDrop(__view.grid);
			}
		}
	}
}